<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Goods extends Controller{
    //根据商品id找到商品，获取用户session
    public  function product_list(){
        $cat_id = Request::instance()->param('cat_id');
        $goods_id=40;
        $res=Db::table('goods')->where("goods_id=$goods_id")->select();
        foreach($res as $key=>$val){
            $goods=$val;
        }
        $user=Session::get('name','think');
        $this->assign('user',$user);
        $this->assign('goods',$goods);
        return view('Product_Detailed');
    }
    //
    public function plus(){
        $attrid=Request::instance()->param('attrid');
        $goods_id=Request::instance()->param('goods_id');
        $num=Request::instance()->param('num');
        $goods=Db::table('goods')->where("goods_id=$goods_id")->find();
        //规格值
        $res=Db::table('good_attr')->where("goods_attr_id in ($attrid)")->field('attr_value')->select();
        foreach($res as $key=>$val){
            $attrval[]=$val['attr_value'];
            $a=implode(',',$attrval);
        }
        //获取登录用户id
        $user=Session::get('name','think');
        $user_id=$user['user_id'];
        $new['goods_sn']=$goods['goods_sn'];
        $new['goods_name']=$goods['goods_name'];
        $new['market_price']=$goods['market_price'];
        $new['goods_id']=$goods_id;
        $new['user_id']=$user_id;
        $new['goods_attr']=$a;
        $new['goods_attr_id']=$attrid;
        $result=Db::table('cart')->insert($new);
        if($result){
            echo json_encode('1');
        }else{
            echo json_encode('2');
        }
    }
}